﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web.Security;

namespace ccgstore.WebApp.Models
{
    public class CcgStoreRoleProvider : RoleProvider
    {
        private readonly CcgStoreRepository _repository;

        public User User { get; private set; }

        public CcgStoreRoleProvider()
        {
            _repository = new CcgStoreRepository();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            try
            {
                var role = _repository.Entities.Roles.FirstOrDefault(r => r.RoleName == roleName);

                return IsUserInRole(username, new List<Role> { role });
            }
            catch (ArgumentException ae)
            {
                // todo: logging
                throw;
            }
        }

        public override string[] GetRolesForUser(string username)
        {
            var role = _repository.GetRolesForUser(username);

            return new [] { role.ToString() };
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public bool IsUserInRole(string username, IEnumerable<Role> roles)
        {
            var user = _repository.GetUser(username);

            return _repository.UserExists(user) && 
                roles.All(role => user.UserRoles.Select(ur => ur.Role).Contains(role));
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }

    //public static class IdentityExtensions
    //{
    //    public static bool IsInRole(this IPrincipal user, string roleName)
    //    {
    //        var roles = Roles.GetRolesForUser(user.Identity.Name);

    //        return roles.Contains(roleName);
    //    }

    //    public static bool IsInRole(this IPrincipal user, params string[] roleNames)
    //    {
    //        return roleNames.All(user.IsInRole);
    //    }
    //}
}